const args = require('minimist')(process.argv.slice(2));
// const { builtinModules } = require('module');
const { resolve } = require('path');
const { build } = require('esbuild');

console.log(args);

const target = args._[0] || 'reactivity';

// const target = args._[0] || 'runtime-dom';

const format = args.f || 'global';

const pkg = require(resolve(__dirname, `../packages/${target}/package.json`));

// iife ： 立即执行函数   cjs: commonJs    esm：浏览器的esModule模块
const outputFormat = format.startsWith('global') ? 'iife' : format === 'cjs' ? 'cjs' : 'esm';

const outfile = resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`);

build({
	entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
	outfile,
	bundle: true, // 把所有的包全部打包到一起
	sourcemap: true,
	format: outputFormat, // 打包的格式
	globalName: pkg?.buildOptions?.name, // 打包全局名字
	platform: format === 'cjs' ? 'node' : 'browser', // 平台的名字
	watch: {
		onRebuild(error) { // 监控文件变化
			if(error) console.log('rebuilt--------')
		}
	}
}).then(() => {
	console.log('watch-----------')
});
